package com.example.crawler.component;

import com.example.crawler.entity.MajorLineEntity;
import com.example.crawler.entity.SchoolInfoEntity;
import com.example.crawler.mapper.MajorLineMapper;
import com.example.crawler.mapper.SchoolInfoMapper;
import com.example.crawler.utils.Test;
import com.xiaoleilu.hutool.http.Header;
import com.xiaoleilu.hutool.http.HttpRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * com.example.crawler.component
 *
 * @author 白雨浓
 * date: 17-10-28 下午2:38
 * content:
 */
@Component
public class MajorLineComponent {
    private static final String USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0";
    private static final String COOKIE = "UM_distinctid=15f620b167d0-0f5b9099fc89a2-72236751-100200-15f620b167e297; CNZZDATA1255363830=60405890-1509173276-null%7C1509190370; Hm_lvt_fd9bf00761167d2e9689ba3d37779881=1509191205,1509192604,1509192609,1509192629; PHPSESSID=jk4iimib1banqqeuvbmuog0ee6; Hm_lpvt_fd9bf00761167d2e9689ba3d37779881=1509192680; w_phone=d76bpypD9w0f3s1vRktAhAKyDIsXY1Y4uIfTEQHKuOkdWYeBInaZow; w_password=9846nRmCLVnd59BUsj%2FMRXsdQItv91%2FO6Db2l2v%2BJmH4d4JVM3GGIt2Py2U; w_tobe=2f6aw%2FNH5kurzjK0CbGdvTNKlYQGoFQL6TVhyOs; w_liulan=1%2C%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6%2Ccollege2%2F5948cbfc5b57c.jpg*568%2C%E5%AE%9C%E6%98%A5%E5%AD%A6%E9%99%A2%2Ccollege2%2F5948cc5a34f25.jpg*569%2C%E8%82%87%E5%BA%86%E5%AD%A6%E9%99%A2%2Ccollege2%2F5948cc5a34f63.jpg";

    @Autowired
    SchoolInfoMapper infoMapper;

    @Autowired
    MajorLineMapper lineMapper;

    public void start() {
        //TODO:下一次从 2500 开始
        List<SchoolInfoEntity> schoolList = infoMapper.select(2500, 1500);

        for (SchoolInfoEntity school : schoolList) {
            handleHtml(school);
        }
    }

    private void handleHtml(SchoolInfoEntity school) {

        int year;
        int page;
        year = 2016;
        List<MajorLineEntity> list = new ArrayList<>();
        while (true) {
            page = 1;
            while (true) {
                String url = "http://www.gaokaoq.com/scores/major.html?type=2&city=17&batch=&year=" + year + "&college=" + school.getSchoolName() + "&p=" + page;

                String html = HttpRequest.get(url)
                        .header(Header.USER_AGENT, USER_AGENT)
                        .header(Header.COOKIE, COOKIE)
                        .header("Cache-Control", "max-age=0")
                        .header("Upgrade-Insecure-Requests", "1")
                        .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                        .execute().body();

                Document doc = Jsoup.parse(html);

                Elements listElements = doc.select("div.line_list");
                if (listElements.size() == 0) break;
                Elements majorLineElements = Jsoup.parse(listElements.html()).select("tr");
                if (majorLineElements.size() == 0) break;

                for (Element element : majorLineElements) {
                    if (element.text().contains("院校名称")) continue;
                    String text = element.text().replace(" / ", "/");
                    String[] items = text.split(" ");
                    if (items.length != 7) continue;
                    list.add(new MajorLineEntity(school.getId(), items[0], items[1], items[2], items[3],
                            items[4], items[5], items[6]));
                    System.out.println("插入学校:" + items[0] + " 年份:" + items[2] + " 专业:" + items[1]);
                }

                if (Jsoup.parse(html).select("a.next").size() == 0)
                    break;
                page++;
            }
            year--;
            if (year == 2013)
                break;
        }
        if (list.size() != 0)
            lineMapper.insertList(list);
    }
}
